﻿using Blm.Utils.Extensions;
using System;
using VisionCloud.Domain.Common;
using VisionCloud.Utility;

namespace VisionCloud.Domain.Extension
{
    public static class VersionExtensions
    {
        /// <summary>
        /// 转int型版本,错误犯规-1
        /// </summary>
        /// <param name="versionStr"></param>
        /// <returns></returns>
        public static int ToVersionIgnoreError(this string versionStr)
        {
            try
            {
                return ToVersion(versionStr);
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// 转int型版本
        /// </summary>
        /// <param name="versionStr"></param>
        /// <returns></returns>
        public static int ToVersion(this string versionStr)
        {
            try
            {
                var splits = versionStr.Split(".", StringSplitOptions.RemoveEmptyEntries);
                if (splits.Length != 3)
                    throw new Exception("");
                return splits[0].ToInt32() * 10000 * 100 + splits[1].ToInt32() * 10000 + splits[2].ToInt32();
            }
            catch
            {
                throw new OperationException($"{StartupServer.CommonLocalizer["YB0451"]}，[1~∞].[1~99].[1~9999]");
            }
        }

        /// <summary>
        /// 转string型版本
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        public static string ToVersion(this int version)
        {
            var result = $"{version / (10000 * 100)}" +
                   $".{(version % (10000 * 100)) / 10000 }" +
                   $".{version % 10000}";
            return result;
        }
    }
}